//
//  DrawViewController.m
//  TodayFirstNews
//
//  Created by 向祖华 on 16/3/7.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "DrawViewController.h"

@interface DrawViewController ()<UIGestureRecognizerDelegate>

@end

@implementation DrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置手势
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.tap setEnabled:NO];
        //设置手势代理
        self.tap.delegate =self;
        //添加手势
        [self.view addGestureRecognizer:self.tap];
    }
    if (!self.swip) {
        self.swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction)];
        self.swip.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:self.swip];
    }

    // Do any additional setup after loading the view from its nib.
}
#pragma mark - 实现自带的方法
-(void)setMidllViewController:(MainPageViewController *)controller{
    //如果传进来的控制器为空则直接返回
    if (!controller) {
        return;
    }
    //设置中间视图控制器
    [self.midllVC.view removeFromSuperview];
    self.midllVC = controller;
    [self.view addSubview:controller.view];
    //如果手势开着，证明抽屉是打开的
//    if (self.tap.enabled) {
//        controller.view.frame = CGRectMake(250, 0, 375, 667);
//    }
    //调用展示中间视图的方法
    [self showMidllViewController:nil];
}
//展示中间视图
-(void)showMidllViewController:(UIViewController *)controller{
    [UIView animateWithDuration:0.4 animations:^{
        self.midllVC.view.frame = CGRectMake(0, 0, 375, 667);
    }];
    //关掉手势
    [self.tap setEnabled:NO];
    //打开交互
    self.midllVC.view.userInteractionEnabled = YES;
}
//展示抽屉
-(void)showLeftViewController:(UIViewController *)controller{
    if (![self.view.subviews containsObject:self.leftVC.view]) {
        [self.view insertSubview:self.leftVC.view atIndex:0];
    }
    [UIView animateWithDuration:0.4 animations:^{
        self.midllVC.view.frame = CGRectMake(300, 0, 375, 667);
        
    }];
    //打开手势
    [self.tap setEnabled:YES];
    //阻断交互
    self.view.userInteractionEnabled = NO;
    
}
//设置向右轻扫弹出抽屉
-(void)swipAction{
    if (self.midllVC.contentScrollV.contentOffset.x ==0) {
        [self showLeftViewController:nil];
    }
}
//轻点手势事件
-(void)tapAction{
    //关闭抽屉
    [self showMidllViewController:nil];
    //关闭手势
    [self.tap setEnabled:NO];
    //打开交互
    self.view.userInteractionEnabled = YES;
    
}
//手势代理方法
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer == self.tap) {
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.midllVC.view]);
    }else if(gestureRecognizer == self.swip){
        return CGRectContainsPoint(self.view.frame, [gestureRecognizer locationInView:self.midllVC.view]);
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
